package com.itqf.controller;

import com.itqf.bean.User;
import com.itqf.exception.LoginException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName: springmvc2103
 *
 * @author: 赵伟风
 * time: 2021/7/5 17:06
 * description: 用户模块对应的controller
 */

@RestController
@RequestMapping("user")
//@CrossOrigin("*")
public class UserController {


    /**
     * produces 设置生产/响应的编码格式
     * @ResponseBody
     *     响应浏览器JSON格式!
     *
     * consumes 设置消费/接受请求的编码格式
     * @RequestBody  request Content-Type:application/json  method Post
     *     接受浏览器JSON格式的数据!
     *
     * @param user
     * @return
     */
    @PostMapping(value = "login",produces = "application/json;charset=utf-8" ,consumes = "application/json;charset=utf-8")
    public Object login(@RequestBody User user) throws LoginException {

        Map map = new HashMap();
        if (user.getAccount() == null || user.getAccount() == "")
        {
            throw  new LoginException("账号为null异常!");
        }else if(user.getPassword() == null || user.getPassword() == ""){
            throw  new LoginException("密码为null异常!");
        }else {

            if ("root".equalsIgnoreCase(user.getAccount()) && "root".equalsIgnoreCase(user.getPassword()))
            {

               map.put("code", 1);
            }else{
                throw  new LoginException("账号或者密码错误!");
            }

        }

        return map;
    }



}
